package com.xiaotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaotao.dto.GoodsDto;
import com.xiaotao.dto.GoodsDto2;
import com.xiaotao.entity.Goods;
import com.xiaotao.entity.GroupBuyDetails;
import com.xiaotao.entity.Merchant;
import com.xiaotao.mapper.GoodsMapper;
import com.xiaotao.mapper.GroupBuyDetailsMapper;
import com.xiaotao.mapper.MerchantMapper;
import com.xiaotao.service.GoodsService;
import com.xiaotao.util.ResponseEnum;
import com.xiaotao.util.ResponseUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private MerchantMapper merchantMapper;

    @Resource
    private GroupBuyDetailsMapper groupBuyDetailsMapper;
    @Override
    public ResponseUtil findAll(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //查询所有商品的信息
        List<Goods> list = goodsMapper.findAll();
        //根据商品中的商户id查询商户到商户的名字
        List<Integer> ids = new ArrayList<>();//商户id列表
        list.forEach(item->{
            ids.add(item.getMerchantId());
        });
        List<Merchant> merchantList =merchantMapper.findByIds(ids);//所有商户信息
        List<GoodsDto2> dto2List = new ArrayList<>();
        for (Merchant merchant : merchantList) {
            for (Goods goods : list) {
                if (merchant.getId().equals(goods.getMerchantId())){
                    GoodsDto2 goodsDto2 = new GoodsDto2();
                    BeanUtils.copyProperties(goods,goodsDto2);
                    goodsDto2.setMerchantName(merchant.getName());
                    dto2List.add(goodsDto2);
                }
            }
        }
        PageInfo<GoodsDto2> goodsDto2PageInfo = new PageInfo<>(dto2List);
        return dto2List!=null?ResponseUtil.get(ResponseEnum.OK,goodsDto2PageInfo):
                ResponseUtil.get(ResponseEnum.FAIL,"查询失败，未找到数据");
    }

    @Override
    public ResponseUtil findByMerchantId(Integer merchantId) {
        List<Goods> list = goodsMapper.findByMerchantId(merchantId);
        return list!=null?ResponseUtil.get(ResponseEnum.OK,list):
                ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil save(Goods goods) {
        int result = goodsMapper.insert(goods);
        return result>0?ResponseUtil.get(ResponseEnum.OK):
                ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil update(Goods goods) {
        int result = goodsMapper.updateByPrimaryKeySelective(goods);
        return result>0?ResponseUtil.get(ResponseEnum.OK):
                ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Transactional
    @Override
    public ResponseUtil delete(Integer id) {
        //先查团购信息表
        List<GroupBuyDetails> byGoodsId = groupBuyDetailsMapper.findByGoodsId(id);
        //删除相关团购信息
        if (byGoodsId!=null&&!byGoodsId.isEmpty()){
            groupBuyDetailsMapper.batchDelete(byGoodsId);
        }
        //删除商品信息
        int result = goodsMapper.deleteByPrimaryKey(id);
        return result>0?ResponseUtil.get(ResponseEnum.OK):
                ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findById(Integer id) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        return goods!=null?ResponseUtil.get(ResponseEnum.OK,goods):
                ResponseUtil.get(ResponseEnum.FAIL);
    }
}
